package com.cloud.tsp.controller;

import com.cloud.api.constants.StrVo;
import com.cloud.api.domain.alarm.*;
import com.cloud.core.util.R;
import com.cloud.log.annotation.Log;
import com.cloud.log.enums.BusinessType;
import com.cloud.log.enums.OperatorType;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.service.IAlarmCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author haohaibo
 * @since 2024-02-21
 */
@RestController
@RequestMapping("/alarm-category")
public class AlarmCategoryController {


    @Resource
    private IAlarmCategoryService alarmCategoryService;
    @Resource(name = "myRedis")
    private RedisTemplate<String, Object> redisTemplate;

    @Tag(name = "v1.0")
    @Operation(summary = "查询报警方案（管理）")
    @PostMapping("/getAlarmCategoryList")
    @Log(title = "报警方案管理-查询报警方案", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public TableDataInfo<AlarmCategory> selectAlarmCategoryList(@RequestBody @Valid QueryAlarmCategory para){
        return alarmCategoryService.selectAlarmCategoryList(para);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "新增或修改报警方案（管理）")
    @PostMapping("/InsertOrUpdate")
    @Log(title = "报警方案管理-新增或修改报警方案", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R InsertOrUpdate(@RequestBody @Validated AlarmCategory para){
        alarmCategoryService.InsertOrUpdate(para);
        return R.ok();
    }


    @Tag(name = "v1.0")
    @Operation(summary = "删除报警方案（管理）")
    @PostMapping("/deleteById")
    @Log(title = "报警方案管理-删除报警方案", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R deleteById(@RequestBody @Valid StrVo para){
        alarmCategoryService.deleteById(para.getId());
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "修改报警短信通知状态（管理）")
    @PostMapping("/updateSmsStatus")
    @Log(title = "报警方案管理-修改报警短信通知状态", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R updateSmsStatus(@RequestBody @Validated StatusUpdate para){
        AlarmCategory alarmCategory = new AlarmCategory();
        alarmCategory.setId(para.getId());
        alarmCategory.setSmsStatus(para.getSmsStatus());
        alarmCategoryService.updateById(alarmCategory);
        AlarmCategory byId = alarmCategoryService.getById(para.getId());
        redisTemplate.opsForHash().put("ALARM-CATEGORY",byId.getCode(),byId);
        return R.ok();
    }
}